package io.itit.ecp.admin.provide.renderer;

import com.google.common.collect.Maps;
import icu.mhb.mybatisplus.plugln.extend.Joins;
import io.itit.ecp.admin.provide.action.req.SceneRoomReq;
import io.itit.ecp.admin.provide.action.resp.EnergyDataResp;
import io.itit.ecp.admin.provide.action.resp.MeterDeviceResp;
import io.itit.ecp.admin.provide.action.resp.SceneRoomResp;
import io.itit.ecp.admin.server.entity.*;
import io.itit.ecp.admin.server.entity.extend.SceneRoomExtEntity;
import io.itit.ecp.admin.server.service.ISceneEdificeService;
import io.itit.ecp.admin.server.service.ISceneFloorService;
import io.itit.ecp.admin.server.service.ISceneProjectService;
import io.itit.ecp.admin.server.service.ISceneRoomService;
import io.itit.grass.common.server.utils.ConvertUtils;
import io.itit.grass.common.utils.Y666Utils;
import io.itit.grass.estelle.starter.parse.AbstractParseRenderer;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.stream.Collectors;

/**
 * <p>
 *
 * @Author：Roy
 * @Description：空间导入解析器
 * @Date：Create by 2023-11-23 10:29:25
 */
@Scope("prototype")
@Component(SceneRoomRenderer.ENTITY_NAME)
public class SceneRoomRenderer extends AbstractParseRenderer<SceneRoomExtEntity> {

    public static final String ENTITY_NAME = "sceneRoomRenderer";

    @Resource
    private ISceneRoomService sceneRoomService;

    @Resource
    private ISceneProjectService sceneProjectService;

    @Resource
    private ISceneEdificeService sceneEdificeService;

    @Resource
    private ISceneFloorService sceneFloorService;

    @Override
    public String entityName() {
        return ENTITY_NAME;
    }

    @Override
    public int getHeadRowIndex() {
        return 1;
    }

    private Set<String> dataSet;
    private Set<String> keySet = new HashSet<>();
    @Override
    public Class<SceneRoomExtEntity> getClazz() {
        return SceneRoomExtEntity.class;
    }


    @Override
    public void processBefore() {
        dataSet = sceneRoomService.lambdaQuery().list()
                .stream().map(t -> t.getSceneProjectId() +
                        t.getSceneEdificeId() +
                        t.getSceneFloorId() +
                        t.getRoomName() +
                        t.getRoomArea())
                .collect(Collectors.toSet());
    }
    private String key(String SceneFloorId, String RoomName) {
        return SceneFloorId + "::" + RoomName;
    }
    @Override
    public void verify(SceneRoomExtEntity entity) {
        addErrorMsg(StringUtils.isBlank(entity.getProjectName()), "项目名称不能为空");
        SceneProjectEntity project = sceneProjectService.lambdaQuery()
                .eq(SceneProjectEntity::getProjectName, entity.getProjectName())
                .one();
        if (project == null){
            addErrorMsg("项目名称不存在");
            return;
        }
        entity.setSceneProjectId(project.getId());

        SceneEdificeEntity edifice = sceneEdificeService.lambdaQuery()
                .eq(SceneEdificeEntity::getSceneProjectId, project.getId())
                .eq(SceneEdificeEntity::getEdificeName, entity.getEdificeName())
                .one();


        if (edifice == null){
            addErrorMsg("楼栋名称不存在");
            return;
        }
        entity.setSceneEdificeId(edifice.getId());


        addErrorMsg(StringUtils.isBlank(entity.getRoomName()), "空间名称不能为空");
//
        SceneFloorEntity floor = sceneFloorService.lambdaQuery()
                .eq(SceneFloorEntity::getSceneEdificeId, edifice.getId())
                .eq(SceneFloorEntity::getFloorName, entity.getFloorName())
                .one();
        if (floor == null){
            addErrorMsg("楼层名称不存在");
        }else{

            entity.setSceneFloorId(floor.getId());
        }
        List<SceneRoomEntity> code = sceneRoomService.lambdaQuery()
                .eq(SceneRoomEntity::getSceneEdificeId,entity.getSceneEdificeId())
                .eq(SceneRoomEntity::getSceneFloorId, entity.getSceneFloorId())
                .eq(SceneRoomEntity::getRoomName,entity.getRoomName())
                .list();
        String key = key(entity.getSceneFloorId(), entity.getRoomName());
        String message = "空间名称【" + entity.getRoomName();

        addErrorMsg(code.size() > 0, message + "】, 数据已存在！");
        addErrorMsg(keySet.contains(key), message + "】, 在导入文件中出现重复！");
        keySet.add(key);
    }


    @Override
    public int processAfter(List<SceneRoomExtEntity> resultList) {
        if (CollectionUtils.isEmpty(resultList)) {
            return 0;
        }
        List<SceneRoomEntity> entities = resultList.stream()
                .map(ser-> ConvertUtils.copy(ser, SceneRoomEntity.class))
                .collect(Collectors.toList());
        sceneRoomService.saveRoomBatch(entities);
        return resultList.size();
    }
}
